from django.db import models
from core.models import CoreModel, table_prefix
from apps.sys.models import DictData
import reversion

"""测试需求对象"""


@reversion.register()
class Requirement(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="requirement_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    module = models.ForeignKey(
        null=True,
        blank=True,
        to="pms.Module",
        related_name="requirement_set",
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="所属模块",
    )
    version = models.ForeignKey(
        null=True,
        blank=True,
        to="pms.Version",
        related_name="requirement_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属版本",
    )
    owner = models.ForeignKey(
        to="sys.User",
        related_name="requirement_owner_set",
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="负责人",
    )
    reviewers = models.ManyToManyField(
        to="sys.User",
        related_name="requirement_reviewers_set",
        db_constraint=False,
        verbose_name="评审人",
    )
    parent = models.ForeignKey(
        null=True,
        blank=True,
        to="Requirement",
        related_name="requirement_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="父需求",
    )
    closed_by = models.ForeignKey(
        null=True,
        blank=True,
        to="sys.User",
        related_name="requirement_closed_by_set",
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="关闭人",
    )
    name = models.CharField(max_length=1024, verbose_name="需求名称")
    origin = models.CharField(max_length=50, default="0", verbose_name="需求来源")
    classify = models.CharField(max_length=50, default="0", verbose_name="需求分类")
    priority = models.CharField(max_length=50, default="0", verbose_name="优先级")
    desc = models.TextField(verbose_name="需求描述")
    criteria = models.TextField(verbose_name="验收标准")
    status = models.CharField(default="draft", max_length=32, verbose_name="状态")
    closed_type = models.CharField(
        max_length=50, null=True, blank=True, verbose_name="需求关闭类型"
    )
    closed_remark = models.TextField(null=True, blank=True, verbose_name="关闭说明")
    review_result = models.CharField(
        max_length=50,
        null=True,
        blank=True,
        verbose_name="评审结果",
    )
    review_remark = models.TextField(null=True, blank=True, verbose_name="评审说明")

    class Meta:
        unique_together = (("product", "name"),)
        db_table = table_prefix + "pms_requirement"
        verbose_name = "需求管理"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)

    def __str__(self) -> str:
        return self.name

    @property
    def classify_label(self):
        return DictData.objects.get(type="pms_req_classify", value=self.classify).label
